import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.TimerTask;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;


public class WeatherReader extends TimerTask
{
	private String cityNum;
	
	public WeatherReader(String cityNum)
	{
		this.cityNum = cityNum;
	}
	
	public void run()
	{
		try
		{
			readWeatherData();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	private void readWeatherData() throws Exception
	{
		HttpConnection httpConn = (HttpConnection)Connector.open("http://pda.gismeteo.ru/"+cityNum+".htm", Connector.READ_WRITE);
		httpConn.setRequestMethod("GET");
		
		int respCode = httpConn.getResponseCode();
		if (respCode != HttpConnection.HTTP_OK) throw new Exception("Server error: "+respCode);
		
		InputStream is = httpConn.openInputStream();
		
		StringBuffer response = new StringBuffer();
		for (;;)
		{
			int read = is.read();
			if (read == -1) break;
			response.append((char)(read & 0xFF));
		}
		
		is.close();
		httpConn.close();
		
		String htmlText = response.toString();
		
		Hashtable result = new Hashtable(); 
		
		String enc = "WINDOWS-1251";
		
		String gmString = "Gismeteo.Ru</a></td>";
		String pString = new String(new char[] {0xC4, 0xE0, 0xE2, 0xEB, 0xE5, 0xED, 0xE8, 0xE5});
		String dayString = new String(new char[] {0xC4, 0xED, 0xE5, 0xEC});
		String nightString = new String(new char[] {0xCD, 0xEE, 0xF7, 0xFC, 0xFE});
		String precipString = new String(new char[] {0xCE, 0xE1, 0xEB, 0xE0, 0xF7, 0xED, 0xEE, 0xF1, 0xF2, 0xFC});
		String windString = new String(new char[] {0xC2, 0xE5, 0xF2, 0xE5, 0xF0});
		String humString = new String(new char[] {0xC2, 0xEB, 0xE0, 0xE6, 0xED, 0xEE, 0xF1, 0xF2, 0xFC});
		
		result.put("city", findText(htmlText, "class=big><center>", null, 1, '<', enc));
		
		result.put("d1",  findText(htmlText, gmString,     "align=center>", 1, '<', null));
		result.put("d1t", findText(htmlText, dayString,    "align=center>", 1, '<', null));
		result.put("n1t", findText(htmlText, nightString,  "align=center>", 1, '<', null));
		result.put("d1o", findText(htmlText, precipString, "/000",      1, '.', enc));
		result.put("p1",  findText(htmlText, pString,      "align=center>", 1, '<', null));
		result.put("h1",  findText(htmlText, humString,    "align=center>", 1, '<', null));
		String w1 = findText(htmlText, windString,   "align=center>", 1, '/', enc);
		w1 = replaceStr(w1, "<br>", " ");
		w1 = (w1.indexOf("штиль") == -1) ? replaceStr(w1, "<", "м/с") : replaceStr(w1, "<", "");
		result.put("w1", w1);
		
		result.put("d2",  findText(htmlText, gmString,     "align=center>", 2, '<', null));
		result.put("d2t", findText(htmlText, dayString,    "align=center>", 2, '<', null));
		result.put("n2t", findText(htmlText, nightString,  "align=center>", 2, '<', null));
		result.put("d2o", findText(htmlText, precipString, "/000",      2, '.', enc));
		result.put("p2",  findText(htmlText, pString,      "align=center>", 2, '<', null));
		result.put("h2",  findText(htmlText, humString,    "align=center>", 2, '<', null));
		String w2 = findText(htmlText, windString,   "align=center>", 2, '/', enc);
		w2 = replaceStr(w2, "<br>", " ");
		w2 = (w2.indexOf("штиль") == -1) ? replaceStr(w2, "<", "м/с") : replaceStr(w2, "<", "");
		result.put("w2", w2);
		
		ScrWeather.loadPhenomenaImage((String) result.get("d1o"), (String) result.get("d2o"));
		ScrWeather.setResults(result);
	}
	
	private String findText(String src, String first, String second, int count, char last, String enc) throws Exception
	{
		int index = src.indexOf(first);
		if (index == -1) return null;
		index += first.length();
		
		if (second != null)
		{
			for (int i = 0; i < count; i++)
			{
				index = src.indexOf(second, index);
				if (index == -1) return null;
				index += second.length();
			}
		}
		StringBuffer buf = new StringBuffer();
		ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
		for (;;)
		{
			if (index >= src.length()) break;
			char chr = src.charAt(index++);
			if (chr == last) return (enc == null) ? buf.toString() : new String(buf2.toByteArray(), enc);
			buf.append(chr);
			buf2.write(chr&0xFF);
		}
		return null;
	}
	
	static public String replaceStr(String original, String from, String to)
	{
		int index = original.indexOf(from);
		if (index == -1)
			return original;
		return original.substring(0, index) + to
				+ original.substring(index + from.length(), original.length());
	}
	
}
